<?php
namespace Admin\Model;
use Think\Model\RelationModel;
class AdminModel extends RelationModel{
  //admin模型类中有关于后台提交数据的自动验证
  protected $_validate=array(
          array('username,email,role_id','identicalNull','',0,'callback'),
          array('username','1,60','{%admin_username_length_error}',0,'length'),
          array('password','4,16','{%admin_length_error_password}',1,'length',1),
          array('role_id','number','{%admin_format_error_role_id}',0),
          array('email','email','{%admin_format_error_email}'),
          array('password','repassword','{%admin_equal_error_rpwd}',0,'confirm'),
          array('username','','{%admin_unique_error_username}',0,'unique'),
        
  );
  protected  $_auto=array(
          array('pwd_hash','randstr',3,'callback'),  
          array('add_time','time',1,'function'),  
  );
  //视图模型
  protected  $_link=array(
      'role'=>array(
          'mapping_type'=>self::BELONGS_TO,
          'class_name'=>'AdminRole',
          'foreign_key'=>'role_id',
      )
  );
  
  //获取随机字符串
  protected function randstr($length=6){
      $hash='';
      $chars= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz@#!~?:-=';
      $max=strlen($chars)-1;
      //做一个随机数播种器种子
      mt_srand((double)microtime()*1000000);
      for($i=0;$i<$length;$i++){
          $hash.=$chars[mt_rand(0, $max)];
      }
      return $hash;
  }
      
  
}